<template>
  <!-- 用户管理 -->
  <div class="box">
    <avue-crud :ref="refName"
               :option="option"
               :page="page"
               :table-loading="tableLoading"
               @row-update="handleUpdate"
               @row-save="handleSave"
               @row-del="rowDel"
               @size-change="sizeChange"
               @current-change="currentChange"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               v-model="form"
               :data="tableData">
      <template #menu="{ size, row, index }">
        <el-button v-if="buttonPermissionsCheck('SYS_USER_SET_ROLE')"
                   @click="setRoleOpen(row)"
                   text type="primary"
                   :size="size">设置角色
        </el-button>
        <el-button v-if="buttonPermissionsCheck('SYS_USER_RESET_PASSWORD')"
                   @click="resetPassword(row)"
                   text
                   type="primary"
                   :size="size">重置密码
        </el-button>
      </template>
    </avue-crud>

    <el-dialog
        title="设置用户角色"
        v-model="dialogVisible"
        :close-on-click-modal="false"
        width="25%">
      <el-select style="width: 100%" v-model="rolesVal" multiple placeholder="请选择">
        <el-option
            v-for="item in roles"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="setRoles()">确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import {option} from '@/table/sys/user/list.js'

const httpService = inject('httpService')

const {
  buttonPermissionsCheck,
  url,
  form,
  page,
  refName,
  tableData,
  tableLoading,
  handleSave,
  handleUpdate,
  rowDel,
  searchChange,
  refreshChange,
  sizeChange,
  currentChange,
  activated,
} = useTableMixins(option, "sys-user")

//当前选中的用户
const obj = ref({})


//角色设置弹框
const dialogVisible = ref(false);

//权限值
const rolesVal = ref([])

//定义角色集合

const roles = ref([])


/**
 * 打开设置角色弹框
 * @param row
 */
const setRoleOpen = row => {
  obj.value = row;
  rolesVal.value = row.roleIds ? row.roleIds.split(",") : [];
  dialogVisible.value = true;
}


/**
 * 设置用户角色
 */
const setRoles = () => {
  httpService.post("/" + url.value + "/setRoles", {
    id: obj.value.id,
    roles: rolesVal.value,
  }).then(res => {
    dialogVisible.value = false;
    ElMessage.success("操作成功！");
    searchChange();
  })
}

/**
 * 重置密码
 * @param row
 */
const resetPassword = (row) => {
  ElMessageBox.confirm('是否确认重置密码？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    httpService.post("/sys-user/resetPassword/" + row.id).then(res => {
      ElMessage.success("操作成功！");
    })
  })

}

/**
 * 数据初始化
 */
(() => {
  //获取所有角色
  httpService.get("/sys-role/findList").then(res => {
    roles.value = res;
  })
})();


onActivated(() => {
  activated()
})
</script>

<style scoped>

</style>
